package com.hifar.test.structure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.test.structure.mapper.HfEnvResPersonTrainPlanMapper;
import com.hifar.test.structure.pojo.HfEnvResPersonTrainPlan;
import com.hifar.test.structure.pojo.VO.HfEnvResPersonTrainPlanExportVO;
import com.hifar.test.structure.pojo.VO.HfEnvResPersonTrainPlanImportVO;
import com.hifar.test.structure.service.IHfEnvResPersonTrainPlanService;
import com.hifar.utils.ExcelImportUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class HfEnvResPersonTrainPlanServiceImpl extends ServiceImpl<HfEnvResPersonTrainPlanMapper, HfEnvResPersonTrainPlan>
        implements IHfEnvResPersonTrainPlanService {
    @Override
    public BusiResponse exportExcel(Map<String, Object> pageInfo) {
        BusiResponse busiResponse = new BusiResponse();
        String ids = (String) pageInfo.get("ids");

        String operationType = (String) pageInfo.get("operationType");

        if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
            ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfEnvResPersonTrainPlanExportVO.class, "导入模板", "培训计划导入数据");
            return busiResponse;
        } else {
            QueryWrapper<HfEnvResPersonTrainPlan> postParam = RequestConver.getPostParam(pageInfo);
            postParam.ne("del_flag", 1);
            postParam.orderByDesc("create_time");
            if (StringUtils.isNotBlank(ids)) {
                postParam.in("id", ids);
            }
            List<HfEnvResPersonTrainPlan> dataList = this.list(postParam);
            List<HfEnvResPersonTrainPlanExportVO> exportData = dataList.stream()
                    .map(item -> {
                        HfEnvResPersonTrainPlanExportVO hfEnvResPersonTrainPlanExportVO = new HfEnvResPersonTrainPlanExportVO();
                        BeanUtils.copyProperties(item, hfEnvResPersonTrainPlanExportVO);
                        Integer trainMode = item.getTrainMode();
//                        培训方式  1 线下  2 线上
                        if (Objects.equals(1, trainMode)) {
                            hfEnvResPersonTrainPlanExportVO.setTrainModeName("线下");
                        } else if (Objects.equals(2, trainMode)) {
                            hfEnvResPersonTrainPlanExportVO.setTrainModeName("线上");
                        }
                        return hfEnvResPersonTrainPlanExportVO;
                    }).collect(Collectors.toList());
            if (exportData != null && exportData.size() > 0) {
                ExcelTemplateUtil.exportExcel(busiResponse, exportData, HfEnvResPersonTrainPlanExportVO.class, "培训计划数据", "培训计划导出");
            } else {
                ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfEnvResPersonTrainPlanExportVO.class, "培训计划数据", "培训计划导出");
            }
            return busiResponse;
        }

    }

    @Override
    public BusiResponse importExcel(MultipartFile file) throws Exception {
        if (file != null && !file.isEmpty()) {
            List<HfEnvResPersonTrainPlanImportVO> importExcel = ExcelImportUtils.importExcel(file.getInputStream(), HfEnvResPersonTrainPlanImportVO.class);
            if (importExcel != null && importExcel.size() > 0) {
                List<HfEnvResPersonTrainPlan> hfEnvResPersonTrainPlanList = importExcel.stream()
                        .map(item -> {
                            HfEnvResPersonTrainPlan hfEnvResPersonTrainPlan = new HfEnvResPersonTrainPlan();
                            BeanUtils.copyProperties(item, hfEnvResPersonTrainPlan);
                            String trainModeName = item.getTrainModeName();
                            if (Objects.equals("线下", trainModeName)) {
                                hfEnvResPersonTrainPlan.setTrainMode(1);
                            } else if (Objects.equals("线上", trainModeName)) {
                                hfEnvResPersonTrainPlan.setTrainMode(2);
                            }
                            return hfEnvResPersonTrainPlan;
                        }).collect(Collectors.toList());

                this.saveBatch(hfEnvResPersonTrainPlanList);
                return BusiResponse.ok("导入成功！");
            }
        }
        return BusiResponse.error("导入失败，原因未知！");
    }
}




